home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / EXAMP9.PC < prev    next >
Encoding:
Text File  |  1995-05-18  |  4.7 KB  |  131 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: examp9.pc.d,v 1.2.720.1 95/02/21 17:49:20 xxxxxxxx: Needtomrg_7_2 $ ";
  4. #endif /* RCSID */
  5.  
  6. /* Copyright (c) 1991 by Oracle Corporation */
  7. /*
  8.    NAME
  9.      examp9.pc - <one-line expansion of the name>
  10.    DESCRIPTION
  11.      <short description of component this file declares/defines>
  12.    PUBLIC FUNCTION(S)
  13.      <list of external functions declared/defined - with one-line descriptions>
  14.    PRIVATE FUNCTION(S)
  15.      <list of static functions defined in .c file - with one-line descriptions>
  16.    RETURNS
  17.      <function return values, for .c file with single function>
  18.    NOTES
  19.      <other useful comments, qualifications, etc.>
  20.    MODIFIED   (MM/DD/YY)
  21.     xxxxxxxx   02/13/95 -  update
  22.     xxxxxxxx   05/12/92 -  Creation 
  23. */
  24. /************************************************************************
  25.  *                                              *
  26.  *  EMBEDDED PL/SQL DEMO                                                *
  27.  *                                                                      *
  28.  *  This program shows the use of host variables.  It prompts for the   *
  29.  *  name of an employee, then executes a PL/SQL block that uses four    *
  30.  *  SELECT statements to get information about the employee.  The       *
  31.  *  information returned includes: job title, hire date, number of      *
  32.  *  people who have served the company longer, salary, number of people *
  33.  *  who have a higher salary, department number, and number of people   *
  34.  *  in that department.                                                 *
  35.  *                                    *
  36.  *  Copyright (c) 1989,1992 by Oracle Corporation.                      *
  37.  ************************************************************************/
  38.  
  39. #include <stdio.h>
  40.     
  41. EXEC SQL BEGIN DECLARE SECTION;
  42.    VARCHAR  empname[11];
  43.    VARCHAR  jobtype[9];
  44.    VARCHAR  hired[9];
  45.    int      salary;
  46.    int      dept;
  47.    int      served_longer;
  48.    int      higher_sal;
  49.    int      total_in_dept;
  50.    VARCHAR  uid[20];
  51.    VARCHAR  pwd[20];
  52. EXEC SQL END DECLARE SECTION;
  53.   
  54. EXEC SQL INCLUDE SQLCA;
  55.   
  56. void sqlerror();
  57. main()
  58. {
  59.       /* Set up userid and password */
  60.    strcpy (uid.arr,"scott");
  61.    uid.len = strlen(uid.arr);
  62.    strcpy (pwd.arr,"tiger");
  63.    pwd.len = strlen(pwd.arr);
  64.   
  65.    printf("\n\n\tEmbedded PL/SQL Demo\n\n");
  66.    printf("Trying to connect...");
  67.       /* Check for SQL errors */
  68.    EXEC SQL WHENEVER SQLERROR DO sqlerror();
  69.       /* Connect to Oracle */
  70.    EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  71.    printf(" connected.\n");
  72.    
  73.    for (;;) /* Loop indefinitely */
  74.    {
  75.       printf("\n** Name of employee? (<CR> to quit)  ");
  76.       gets(empname.arr);               /* Get the name      */
  77.       if (strlen(empname.arr) == 0)    /* No name entered,  */
  78.       {
  79.          EXEC SQL COMMIT WORK RELEASE; /* so log off Oracle */
  80.          exit(0);                      /* and exit program  */
  81.       }
  82.       empname.len = strlen(empname.arr);
  83.       jobtype.len = 9;
  84.       hired.len = 9;
  85.  
  86.       /* ----- Begin PL/SQL block ----- */
  87.       EXEC SQL EXECUTE
  88.       BEGIN
  89.          SELECT job, hiredate, sal, deptno
  90.             INTO :jobtype, :hired, :salary, :dept FROM emp
  91.             WHERE ename = UPPER(:empname);
  92.             /* Get number of people whose length *
  93.              * of service is longer              */
  94.          SELECT COUNT(*) INTO :served_longer FROM emp
  95.             WHERE hiredate < :hired;
  96.             /* Get number of people with a higher salary */
  97.          SELECT COUNT(*) INTO :higher_sal FROM emp
  98.             WHERE sal > :salary;
  99.             /* Get number of people in same department */
  100.          SELECT COUNT(*) INTO :total_in_dept FROM emp
  101.             WHERE deptno = :dept;
  102.       END;
  103.       END-EXEC;
  104.       /* ----- End PL/SQL block ----- */
  105.  
  106.          /* Null-terminate character strings returned by Oracle */
  107.       jobtype.arr[jobtype.len] = '\0';
  108.       hired.arr[hired.len] = '\0';
  109.          /* Display the information */
  110.       printf("\n%s's job is: %s\n", empname.arr, jobtype.arr);
  111.       printf("Hired on: %s\n", hired.arr);
  112.       printf("    %d people have served longer\n", served_longer);
  113.       printf("Salary is: %d\n", salary);
  114.       printf("    %d people have a higher salary\n", higher_sal);
  115.       printf("Department number is: %d\n", dept);
  116.       printf("    %d people in the department\n", total_in_dept);
  117.    }  /* End of loop */
  118. }  
  119.  
  120. void sqlerror()
  121. {
  122.       /* Avoid infinite loop if rollback causes an error */ 
  123.    EXEC SQL WHENEVER SQLERROR CONTINUE;
  124.    printf("\nOracle error detected:\n");
  125.       /* Print error message and disconnect from Oracle */
  126.    printf("\n%.70s\n", sqlca.sqlerrm.sqlerrmc);
  127.    EXEC SQL ROLLBACK WORK RELEASE;
  128.    exit(1);
  129. }
  130.  
  131.